import java.sql.SQLOutput;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
 * @作者: xiaoxun
 * @日期: 2025/3/10 19:15
 * @描述:Stream流
 */
public class StreamDemo01 {
	public static void main(String[] args) {
	//体验Stream流
		List<String> list=new ArrayList<>();
		list.add("张三丰");
		list.add("张无忌");
		list.add("赵敏");
		list.add("周芷若");
		list.add("张强");
		//1、需要将姓张的放到一个新集合中去
		List<String> newList=new ArrayList<>();
		for (String s:list){
			if (s.startsWith("张")){
				newList.add(s);
			}
		}
		for (String s:newList){
			System.out.println(s);
		}
		//2、使用Stream流改进
		List<String> newLIst2=list.stream().filter(new Predicate<String>() {
			@Override
			public boolean test(String s) {
				return s.startsWith("张") && s.length()==3;
			}
		}).collect(Collectors.toList());
		//Lambda表达式简化
		List<String> newList3=list.stream().filter(s -> s.startsWith("张")&&s.length()==3).collect(Collectors.toList());
		System.out.println(newLIst2);
//		Stream流常用方法
		//1、获取Stream流
		Collection<String> list01=new ArrayList<>();
		Collections.addAll(list01,"张无忌","张三丰","赵敏","周芷若","张强");
		Stream<String> s1=list01.stream();
		//2、获取Map集合的Stream流
		Map<String,Integer> map=new HashMap<>();
		//获取键流
		Stream<String> keys=map.keySet().stream();
		//获取值流
		Stream<Integer> values=map.values().stream();
		//获取键值对流
		Stream<Map.Entry<String,Integer>> entryStream=map.entrySet().stream();
		//3、获取数组的stream流
		String[] names={"张无忌","张三丰","赵敏","周芷若","张强"};
		Arrays.stream(names);//获取数组的stream流方法一
		Stream<String> names2=Stream.of(names);//获取数组的stream流方法二

//		流常用方法
		//1、对流中的数据进行过滤
		List<String> list2=new ArrayList<>();
		Collections.addAll(list2,"张无忌","张三丰","赵敏","周芷若","张强");
		//1、对流中的数据进行过滤
		list2.stream().filter(new Predicate<String>() {
			@Override
			public boolean test(String s) {
				return s.startsWith("张")&&s.length()==3;
			}
		}).forEach(new Consumer<String>() {
			@Override
			public void accept(String s) {
				System.out.println(s);
			}
		});
		//2、准备一个集合排序
		List<Movie> movies=new ArrayList<>();
		movies.add(new Movie("哪吒2魔童闹海",41.2,"饺子"));
		movies.add(new Movie("摔跤吧，爸爸",35.0,"阿尔米汉"));
		movies.add(new Movie("阿甘正传",40.2,"汤姆汉克斯"));
		movies.add(new Movie("三傻宝莱坞",20.3,"阿尔米韩"));
		movies.add(new Movie("三傻宝莱坞2",20.3,"阿尔米韩1"));
		movies.add(new Movie("阿甘正传3",40.2,"汤姆汉克斯"));
//		movies.stream().sorted().forEach(System.out::println);
//		System.out.println(movies);
		//使用forEach循环遍历
		movies.stream().sorted(new Comparator<Movie>() {
			@Override
			public int compare(Movie o1, Movie o2) {
				return (int) (o1.getScore()-o2.getScore());
			}
		}).forEach(System.out::println);
		//3、取前几个数据
		System.out.println("===========================================");
		movies.stream().limit(3).forEach(System.out::println);
		//4、跳过前三个
		System.out.println("===========================================");
		movies.stream().skip(3).forEach(System.out::println);
		//5、去除重复
		System.out.println("===========================================");
		movies.stream().sorted((o1, o2) -> (int) (o1.getScore()-o2.getScore())).distinct().forEach(System.out::println);
		//6、把流上的数据加工成新数据
		System.out.println("===========================================");
		movies.stream().map(m->m.getName()+"==>"+m.getScore()).forEach(System.out::println);
		//7、合并流
		Stream<String> s01=Stream.of("张三","李四","万五");
		Stream<String> s02=Stream.of("张五","李三","万六");
		Stream<String> allString=Stream.concat(s01,s02);
		System.out.println(allString.count());
		//8、终结方法
		//（1）取较大值
		Optional<Movie> max=movies.stream().max((o1, o2) -> (int) (o1.getScore()-o2.getScore()));
		Movie m=max.get();
		System.out.println(m);
		//(2)取较小值
		Movie min= movies.stream().min((o1, o2) -> (int) (o1.getScore()-o2.getScore())).get();
		System.out.println(min);
		//收集stream流
//		（1）收集到list集合
		List<String> newList02=list.stream().filter(new Predicate<String>() {
			@Override
			public boolean test(String s) {
				return s.startsWith("张") && s.length()==3;
			}
		}).collect(Collectors.toList());
		System.out.println(newList02);
		//(2)手机到Set集合
//		List<String> newList03= (List<String>) list.stream().filter(s -> s.startsWith("张") && s.length()==3).collect(Collectors.toSet());
		System.out.println(newList02);

		//（3）收集到数组
//			toArray()

	}
}
